package com.net
{
	import com.net.Event.HttpEvent;
	import com.net.http.HttpRequest;
	import com.net.requesthandler.IRequestHandler;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ServerSocketConnectEvent;
	import flash.net.ServerSocket;
	import flash.net.Socket;
	import flash.utils.Dictionary;

	public class HttpServer extends EventDispatcher
	{
		//-----------------------------------------------------
		//                    static
		//-----------------------------------------------------
		private static var httpserver:HttpServer;
		public static function getHttpServer():HttpServer{
			if(httpserver==null){
				httpserver=new HttpServer();
			}
			return httpserver;
		}
		//-----------------------------------------------------
		
		
		/**
		 * 获取虚拟目录地址
		 */
		public static function getVD():String{
			return httpserver.virtualDirectory;
		}
		
		/**
		 * 获取默认页面地址
		 */
		public static function getDefaultPage():String{
			return httpserver.defaultPage;
		}
		
		/**
		 * 获取处理相关请求连接的处理对象
		 * 返回的将是实例化对象
		 */
		public static function getIRequestHandler(name:String):IRequestHandler{
			var type:Class=httpserver.service[name] as Class;
			if(type==null){
				return null;
			}
			var rh:IRequestHandler=new type();
			return rh;
		}
		//----------------------------------------------------
		//----------------------------------------------------
		
		
		private var server:ServerSocket;
		
		private var bindPort:int;
		
		private var bindHost:String;
		
		private var virtualDirectory:String;//虚拟目录
		
		private var defaultPage:String;//默认页面
		
		private var list:Vector.<HttpRequest>;//连接组
		
		private var service:Dictionary;//用于保存相关服务的对象
		
		/**
		 * 一个简单的基于http协议的服务器
		 * 可以提供简易WEB服务器功能
		 * 
		 * 可以绑定到端口 指定虚拟目录  可以自定义处理相关请求
		 */
		public function HttpServer()
		{
			server=new ServerSocket();
			list=new Vector.<HttpRequest>();
			service=new Dictionary();
		}
		
		/**
		 * 绑定到端口
		 * 可以指定虚拟目录(vd)
		 */
		public function bind(host:String="127.0.0.1",port:int=80,vd:String="",_default:String="index.html"):void{
			bindHost=host;
			bindPort=port;
			virtualDirectory=vd;
			defaultPage=_default;
		}
		
		/**
		 * 启动服务器
		 */
		public function start():void{
			if(server.listening){
				return;
			}
			try{
				server.bind(bindPort,bindHost);
				server.addEventListener(ServerSocketConnectEvent.CONNECT,on_connect);
				server.listen();
			}catch(error:Error){
				//绑定失败
			}
		}
		
		/**
		 * 关闭服务器
		 */
		public function stop():void{
			if(server.listening){
				server.close();
			}
			for each(var t:HttpRequest in list){
				t.close();
				t.removeEventListener(Event.CLOSE,on_close);
			}
			list.splice(0,list.length);
		}
		
		/**
		 * 添加一个与链接相关联的服务对象
		 * 参数1：指定的服务连接
		 * 参数2：用来处理对象的类  必须实现IRequestHandle
		 */
		public function addCustomServer(requestLink:String,automateClass:Class):void{
			service[requestLink]=automateClass;
		}
		
		//新连接了连接到服务器端口
		private function on_connect(event:ServerSocketConnectEvent):void{
			var client:HttpRequest=new HttpRequest(event.socket);
			client.addEventListener(HttpEvent.SESSION_CLOSE,on_close);
			list.push(client);
		}
		
		//当有连接出现失效时  进行一次清理
		private function on_close(event:Event):void{
			for(var i:int=list.length-1;i>=0;i--){
				if(!list[i].isConnect){
					list[i].removeEventListener(HttpEvent.SESSION_CLOSE,on_close);
					list.splice(i,1);
				}
			}
		}
	}
}